package com.jxmcloud.business.cut.service.impl.logic;

import cn.hutool.json.JSONUtil;
import com.jxmcloud.business.cut.feign.vip.VipClientNew;
import com.jxmcloud.business.cut.service.logic.IPayHandle;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.zhongwei.pay.param.PayParam;
import com.zhongwei.pay.param.PayXuanLeParam;
import com.zhongwei.pay.transfer.IPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class PayHandleImpl implements IPayHandle {
    @Autowired
    private VipClientNew vipClient;
    @Autowired
    private IPayService payService;

    @Override
    public boolean checkUserPassword(PlayerInfoVo player, String password) {
        // 判断用户支付密码是否正确
        String userPayToken = vipClient.getUserPayToken(player.getBase().getXlId(), password);
        String code = JSONUtil.parseObj(userPayToken).getStr("code");
        if(!StringUtils.isEmpty(code) && code.equals("1")) {
            //error = "支付密码错误";
            return false;
        }
        return true;
    }

    @Override
    public boolean checkUserBalance(PlayerInfoVo player, Long payPrice) {
        // 判断余额是否足够
        long xuanLeBalance = payService.getXuanLeBalance(player.getBase().getXlId());
        if (xuanLeBalance < payPrice) {
            //error = "余额不足";
            return false;
        }
        return true;
    }

    @Override
    public boolean tansferMoney(Long xuanleForm, Long xuanleTo, Long payPrice) {
        try {
            payService.transfer(PayParam.xuanLe(null,
                    xuanleForm,      //支付用户id
                    xuanleTo,        //平台id
                    payPrice,        //支付玄乐金额,单位是：分
                    //游戏专用参数
                    new PayXuanLeParam(502, 503, "砍一刀购买加速礼包", 441, 44, "砍一刀购买加速礼包失败退回")));

        } catch (Exception e) {
            log.error("砍一刀购买加速礼包转账玄乐失败，userId={}" + xuanleTo, e);
            return false;
        }
        return true;
    }
}
